package org.example.mq;

import org.example.config.RabbitConfig;
import org.example.entity.Book;
import org.example.entity.Order;
import org.example.service.BookService;
import org.example.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class RecelverImpl {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BookService bookService;

    @RabbitListener(queues = RabbitConfig.S59_QUEUE_1)
    public void receive1(Order order){
        System.out.println(RabbitConfig.S59_QUEUE_1 + "收到消息：" + order);
        System.out.println("收到订单创建消息" + order.getOrderId());
        Book book = bookService.findById(order.getBookId());
        order.setMoney(book.getMoney().multiply(new BigDecimal("0.1")));
        orderService.createOrder(order);
    }

//    @RabbitListener(queues = RabbitConfig.S59_QUEUE_2)
//    public void receive2(Object  message){
//        System.out.println(RabbitConfig.S59_QUEUE_2 + "收到消息：" + message);
//    }

    @RabbitListener(queues = RabbitConfig.S59_QUEUE_2)
    public void receive2(Order order){
        System.out.println("收到订单修改库存" + order.getOrderId());
        System.out.println("书籍id" + order.getBookId());
        bookService.modifyStock(order.getBookId(), -1);
    }


}
